<?php
namespace App\Adshopuser\Controllers;
use App\Adshopuser\Models\BShopGoodsCategory;
use App\Adshopuser\Models\BshopUsersInfo;
use Common\ControllerAdshopBase;
use Common\Models\AppConfig;
use Common\Models\BShopUsers;
use Common\Models\FVerificationCodes;
use Common\Models\User;

class ShopinfoController extends ControllerAdshopBase
{

    public function initialize()
    {
        parent::initialize();
    }

    //店铺基本信息
    public function shopbaseAction(){
        $params['shop_id'] = 5;//$this->user_admin['id'];
        $BShopUserInfoDb = new BshopUsersInfo();
        $shop_info = $BShopUserInfoDb->get_shop_goods_list($params);
        $BShopGoodsCategoryDb = new BShopGoodsCategory();
        $bshopgoodscategory = $BShopGoodsCategoryDb->findFirstById($shop_info['category_id']);
        $shop_info['category_name'] = $bshopgoodscategory ? $bshopgoodscategory->category_name : '';
        $shop_info['card_img_arr'] = $shop_info['id_card_img'] ? explode(',',$shop_info['id_card_img']) : [];
        $BShopUsersDb = new BShopUsers();
        $bshopusers = $BShopUsersDb->findFirstById($this->user_admin['id']);
        $shop_info['created_at'] = $bshopusers ? $bshopusers->created_at : '';
        $this->ok($shop_info);
    }

    // 修改店铺的基本信息
    public function update_shopinfobaseAction(){
        $FAppConfigDb = new AppConfig();
        $app_config = $FAppConfigDb->find_list("update_shop_info");
        if(!isset($app_config['shop_info_update_info'])){
            $app_config['shop_info_update_info'] = 5;
        }

        $this->dbMaster->begin();
        $BShopUserInfoDb = new BshopUsersInfo();
        $bshopinfo = $BShopUserInfoDb->findFirstByShopId($this->user_admin['id']);
        if(!$bshopinfo){
            $this->fail(-1,'没有找到店铺的基本信息');
        }
        if(date('Y-m') == $bshopinfo->updated_month){
            if($bshopinfo->update_count >= $app_config['shop_info_update_info']){
                $this->fail(-1,"修改次数达到上限-{$bshopinfo->update_count}次");
            }
        }

        $bshopinfo->update_count = $bshopinfo->update_count + 1;
        $bshopinfo->updated_month = date('Y-m');
        $bshopinfo->latitude_longitude = $this->params['latitude_longitude'];
        $bshopinfo->shop_avatar = $this->params['shop_avatar'];
        $bshopinfo->shop_img = $this->params['shop_img'];
        $bshopinfo->shop_introduction = $this->params['shop_introduction'];
        $bshopinfo->shop_name = $this->params['shop_name'];
        if(!$bshopinfo->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'修改店铺信息失败');
        }

        $BShopUsersDb = new BShopUsers();
        $bshopusers = $BShopUsersDb->findFirstById($this->user_admin['id']);
        if(!$bshopusers){
            $this->dbMaster->rollback();
            $this->fail(-1,'没有找到店铺用户信息');
        }
        $bshopusers->updated_at = date('Y-m-d H:i:s');
        $bshopusers->username = $this->params['shop_name'];
        $bshopusers->introduction = $this->params['shop_introduction'];
        $bshopusers->avatar = $this->params['shop_avatar'];
        if(!$bshopusers->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'修改店铺信息失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }

    /**
     * 获取分类
     */
   public function shop_categoryAction(){
       if(!isset($this->params['pid']) || empty($this->params['pid'])){
           $this->params['pid']=0;
       }
        $BShopGoodsCategoryDb = new BShopGoodsCategory();
        $get_category = $BShopGoodsCategoryDb->get_list($this->params['pid']);
        $this->ok($get_category);
    }


    //根据地址获取经纬度
    public function get_addresslngAction(){
        if(!isset($this->params['shop_address']) || empty($this->params['shop_address'])){
            $this->fail(-1,'请输入您的地址');
        }
        $key        = 'd35ccb7840367a7ace5062d0566f15c7';
        $url        = "https://restapi.amap.com/v3/geocode/geo?key={$key}&address={$this->params['shop_address']}";
        $res        = json_decode(file_get_contents($url), true);
        $latitude_longitude = '';
        if(isset($res['status']) && $res['status']==1) {
            if (isset($res['geocodes'][0]['location'])) {
                $latitude_longitude = $res['geocodes'][0]['location'];
            }
        }
        if($latitude_longitude){
            $result['latitude_longitude'] = explode(',',$latitude_longitude);
        }else{
            $result['latitude_longitude']=[116.403963,39.915119];
        }
        $this->ok($result);
    }

    /**
     * 获取登录账号
     */
    public function shoploginuserAction(){
        $BShopUsersDb = new BShopUsers();
        $bshopusers = $BShopUsersDb->findFirstById($this->user_admin['id']);
        $result=[];
        if($bshopusers){
            $result = $bshopusers->toarray();
            $UserDb = new User();
            $user_info = $UserDb->find_by_id($result['user_id']);
            $result['phone'] = $user_info ? $user_info->phone : '';
        }
        $this->ok($result);
    }


    /**
     * 修改登录账号
     */
    public function update_shopuserAction(){
        if(!isset($this->params['password']) || empty($this->params['password'])){
            $this->fail(-1,'密码不能为空');
        }else{
            $pagch = '/([a-zA-Z0-9!@#$%^&*()_?<>{}]){8,18}/';
            if(!preg_match($pagch,$this->params['password'])){
                $this->fail(-1,'长度在8-18位');
            }
        }
        if(!isset($this->params['re_password']) || empty($this->params['re_password'])){
            $this->fail(-1,'重复密码不能为空');
        }
        if($this->params['password'] != $this->params['re_password']){
            $this->fail(-1,'两次密码输入不一致');
        }
        if(!isset($this->params['msg_code']) || empty($this->params['msg_code'])){
            $this->fail(-1,'验证码不能为空');
        }
        $fverficationcaodesDb = new FVerificationCodes();
        $fverficationcaodes = $fverficationcaodesDb->check_phone($this->params['phone'],$this->params['msg_code']);
        if($fverficationcaodes != true){
            $this->fail(-1,$fverficationcaodes);
        }
        $FShopUserDb = new BShopUsers();
        $b_shop_user = $FShopUserDb->findFirstById($this->user_admin['id']);
        $b_shop_user->password = md5($this->params['password']);
        $b_shop_user->updated_at = date('Y-m-d H:i:s');
        if(!$b_shop_user->save()){
            $this->fail(-1,'修改出错');
        }
        $this->ok([]);

    }

}
